package kr.ac.kongju.gedl.cmmn.tag;

import javax.servlet.http.HttpSession;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang.StringUtils;


public class LocationTag extends TagSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -9200870389322778179L;
	
	private String levelId;
	private String setId;
	private String position;
	private String levelUri;
	private String itemId;
	private String prevId;
	private String nextId;
	private String parentId;
	private String resultParam;
	private String totalCount;
	
	

	public String getLevelId() {
		return levelId;
	}



	public void setLevelId(String levelId) {
		this.levelId = levelId;
	}



	public String getSetId() {
		return setId;
	}



	public void setSetId(String setId) {
		this.setId = setId;
	}



	public String getPosition() {
		return position;
	}



	public void setPosition(String position) {
		this.position = position;
	}



	public String getLevelUri() {
		return levelUri;
	}



	public void setLevelUri(String levelUri) {
		this.levelUri = levelUri;
	}



	public String getItemId() {
		return itemId;
	}



	public void setItemId(String itemId) {
		this.itemId = itemId;
	}



	public String getPrevId() {
		return prevId;
	}



	public void setPrevId(String prevId) {
		this.prevId = prevId;
	}



	public String getNextId() {
		return nextId;
	}



	public void setNextId(String nextId) {
		this.nextId = nextId;
	}



	public String getParentId() {
		return parentId;
	}



	public void setParentId(String parentId) {
		this.parentId = parentId;
	}



	public String getResultParam() {
		return resultParam;
	}



	public void setResultParam(String resultParam) {
		this.resultParam = resultParam;
	}
	
	public String getTotalCount() {
		return totalCount;
	}



	public void setTotalCount(String totalCount) {
		this.totalCount = totalCount;
	}



	public int doStartTag() throws JspException {
		try {
			
            JspWriter jw = pageContext.getOut();
            StringBuffer sb = new StringBuffer();
            if(!StringUtils.isEmpty(setId) && !StringUtils.isEmpty(position)) {
            	int pos = Integer.parseInt(position);
            	int totCnt = Integer.parseInt(totalCount);
            	int prevPosition = pos - 1;
            	int nextPosition = pos + 1;
	            sb.append("<p class=\"btn_rst\">");
	            sb.append("<a href=\"javascript:fncSearchResultList('"+itemId+"', '"+setId+"')\"><img src=\"/m/images/button/btn_rst_list.gif\" alt=\"\"/></a>\n");
	            
	            String searchPrevFunc = pos > 0  ? levelUri+"?itemInfo.itemId="+itemId+"&itemInfo.setId="+setId+"&position="+prevPosition : "#";
	            String searchNextFunc = pos + 1 < totCnt  ? levelUri+"?itemInfo.itemId="+itemId+"&itemInfo.setId="+setId+"&position="+nextPosition : "#";
	    		sb.append("<a href=\"javascript:goDir('"+searchPrevFunc+"')\"><img src=\"/m/images/button/btn_rst_prev.gif\" alt=\"\"/></a>\n");
	    		sb.append("<a href=\"javascript:goDir('"+searchNextFunc+"')\"><img src=\"/m/images/button/btn_rst_next.gif\" alt=\"\"/></a>\n");
	    		sb.append("</p>");
            }
    		if(!StringUtils.isEmpty(levelId)) {
    			String prevFunc = StringUtils.isEmpty(prevId) ? "#" : levelUri+"?levelId="+prevId+resultParam;
    			String upFunc = StringUtils.isEmpty(parentId) ? levelUri+"?itemInfo.itemId="+itemId : levelUri+"?levelId="+parentId+resultParam;;
    			String nextFunc = StringUtils.isEmpty(nextId) ? "#" : levelUri+"?levelId="+nextId+resultParam;;
    			
    			sb.append("<a href=\"javascript:goDir('"+prevFunc+"')\"><img src=\"/m/images/button/btn_m_prev.gif\" alt=\"\"/></a>\n");
	    		sb.append("<a href=\"javascript:goDir('"+upFunc+"')\"><img src=\"/m/images/button/btn_m_up.gif\" alt=\"\"/></a>\n");
	    		sb.append("<a href=\"javascript:goDir('"+nextFunc+"')\"><img src=\"/m/images/button/btn_m_next.gif\" alt=\"\"/></a>\n");
    		}
            
    		jw.print(sb.toString());
    		
        } catch (Exception ex) {
            throw new JspTagException(ex.getMessage());
        }
                
        return SKIP_BODY; 
	}
	
}
